<template>
  <div class="app-container">
    <el-dialog :title="'关联生产BOM'+'（'+form.orderNo+'）'" :visible.sync="dialogVisible" width="50%" append-to-body>
      <el-form ref="form" :model="form" v-loading="loading">
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="BOM名称" prop="produceBomId" required>
              <el-select  v-model="form.produceBomId" style="width: 100%" filterable @change="changeBom">
                <el-option
                  v-for="item in bomList"
                  :key="item.id"
                  :label="item.bomName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="myTitle">物料信息</div>
      <el-table
        :data="list"
        maxHeight="500"
      >
        <el-table-column type="index" label="序号" align="center"></el-table-column>
        <el-table-column prop="materialName" label="物料名称" align="center"/>
        <el-table-column prop="skuName" label="物料规格" align="center"/>
        <el-table-column prop="materialType" label="物料类型" align="center">
          <template slot-scope="scope">
            <dict-tag  :options="dict.type.sys_material_type" :value="scope.row.materialType"/>
          </template>
        </el-table-column>
        <el-table-column prop="master" label="主材料" align="center" >
          <template slot-scope="scope">
            <dict-tag  :options="dict.type.sys_yes_no" :value="scope.row.master"/>
          </template>
        </el-table-column>
        <el-table-column prop="quantity" label="数量" align="center">
          <template slot-scope="scope">
            <el-input type="number" v-model="scope.row.quantity" :min="0" placeholder="请输入"></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="size" label="尺寸" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.size" placeholder="请输入"></el-input>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="dialogOk">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {lisProduceBom} from "../../../../api/produceMng";
import {okSaleOrderBom} from "../../../../api/imsMng";

export default {
  dicts:['sys_material_type','sys_yes_no'],
  data() {
    return {
      loading: false,
      dialogVisible: false,
      form: {},
      list: [],
      bomList: []
    }
  },
  methods: {
    show(form) {
      this.list = []
      this.form = {}
      this.resetForm('form')
      this.form.id=form.id
      this.form.orderNo=form.orderNo
      this.$set(this.form,'produceBomId',form.produceBomId)
      this.form.produceBomItems=form.produceBomItems
      if(this.form.produceBomItems){
        this.list=this.form.produceBomItems
      }
      console.log()
      this.dialogVisible = true
      this.load()
    },
    load(){
      this.loading=true
      lisProduceBom({pageSize:-1}).then(res=>{
        this.bomList=res.rows
      }).finally(e => {
        this.loading = false
      })
    },
    changeBom(val) {
      let obj=this.bomList.filter(e=>e.id==val)[0]
      this.list =obj.itemList
    },
    dialogOk() {
      this.$refs.form.validate(e => {
        if (e) {
          this.loading=true
          this.form.produceBomItems=this.list
          okSaleOrderBom(this.form).then(res => {
            this.$emit('ok')
            this.dialogVisible = false
            this.$message.success('关联成功')
          }).finally(e => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>
